package br.com.DAO;

import java.util.ArrayList;
import java.util.List;

import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import br.com.Bean.DisciplinaBean;
import br.com.Bean.ForumBean;
import br.com.Bean.UsuarioBean;

public class ForumDAO {
	
	public void salva(ForumBean forumBean,DisciplinaBean disciplina,UsuarioBean usuario){
		ParseObject forum = new ParseObject("Forum");
		forum.put("titulo", forumBean.getTitulo());
		forum.put("conteudo", forumBean.getConteudo());
		forum.put("idUsuario", usuario.getId());
		forum.put("idDisciplina", disciplina.getId());
		forum.put("data", forumBean.getData());
		forum.saveInBackground();
	}
	
	public void deleta(ForumBean forumBean){
		 List<ParseObject> todos = null;
		 List<DisciplinaBean> listaTodos = new ArrayList<>();
		
		ParseQuery query = new ParseQuery("Forum");
		query.whereEqualTo("objectId", forumBean.getId());
		 try {
				todos = query.find();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 
		 if(todos != null){
			 for (ParseObject todo : todos) {
               ForumBean f = new ForumBean();
               	f.setId(todo.getObjectId());
               	todo.deleteInBackground();
               	System.out.println("DELETOU");
         //       listaTodos.add(d);
            }
		 }
	}
	
	public List<ForumBean> buscaPeloIdDisciplina(DisciplinaBean disciplina) {
		// TODO Auto-generated method stub
		 ComentarioDAO comentarioDAO = new ComentarioDAO();
		 UsuarioDAO usuarioDAO = new UsuarioDAO();
		 List<ParseObject> todos = null;
		 List<ForumBean> listaTodos = new ArrayList<>();
		
		ParseQuery query = new ParseQuery("Forum");
		query.whereEqualTo("idDisciplina", disciplina.getId());
		 try {
				todos = query.find();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 
		 if(todos != null){
			 for (ParseObject todo : todos) {
				 ForumBean f = new ForumBean();
				 	f.setId(todo.getObjectId());
				 	f.setConteudo(todo.getString("conteudo"));
				 	f.setData(todo.getDate("data"));
				 	f.setListaComentario(comentarioDAO.buscaPorIdForum(todo.getObjectId()));
				 	f.setTitulo(todo.getString("titulo"));
				 	f.setUsuarioBean(usuarioDAO.buscaPeloId(todo.getString("idUsuario")));
				
				 listaTodos.add(f);
             }
		 }
		 
		return listaTodos;
	}
}
